class ScoreMgrController {
 
    def index = { redirect(action: "selectTerm", controller: 'scoreMgr',params: params)}
   // 批量输入成绩入口，选择学期
   // 初始化学期，确定班级
//    def batch = {
//        if (params.id) {  
//            def exam = Exam.get(params.id)
//            [ classes : exam.classes , term : exam.term]
//        }else{
//            flash.message = "必须首先确定班级。"
//        }
//    }
    // 保存选择的学期，开始批量录入界面
    // 获取学生成绩，回填页面
    def selectTerm = { 
        def exam = Exam.get(params.id)
  
        def classes = exam.classes

        // ==========================
        def scoreArray = []
        def scores = Score.findAllByExam(exam)
      
        classes?.student?.each{ std->
            scoreArray.add([std , 
                scores.find{ 
                    it.student?.id == std.id }
                    ])
        }
        log.info "selectTerm history size : ${scoreArray.size()}"
        
        [exam : exam ,
            'history': scoreArray]
    }

//循环保存学生的成绩

    def batchSave = {
        def students = params.students
        if (!students in Object[]) {
            students = [params.students]
        }
        def exam = Exam.get(params.id)

        def score
        // 循环获得学生，
        students.eachWithIndex{ sid ,  index ->
   
            if (params["score" + sid] && params["score" + sid] != '') { //确定已经录入了分数
                
                float scoreValue = Float.parseFloat (params["score" + sid ])

                if (scoreValue > 0) {

                    if (params["scoreid" + sid ]) {//确认是更新还是添加
                        score = Score.get(params["scoreid" + sid ])
                    } else{
                        score = new Score()
                    }
                    score.student = Student.get(sid) 
                    score.score = scoreValue 
                    score.exam = exam
                    score.save()

                }
            }
        }
        flash.message = "成绩录入完成！"
        return redirect(action: "list", controller: 'exam', params : [id: exam?.classes?.id ,course : exam?.course ]) 
    }

}
//    Student student
//    String course // 科目
//    float score // 分数
//    String term//学期
//    int grade // 年级 (一年级....)
//    int team // 班
//    Classes classes //班级

